﻿// =====================================================================
//
//  This file is part of the Microsoft Dynamics CRM SDK code samples.
//
//  Copyright (C) Microsoft Corporation.  All rights reserved.
//
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
// =====================================================================
#region

using System;
using System.Diagnostics;
using System.Globalization;
using System.Text;

#endregion

namespace Microsoft.Xrm.Sdk.Client
{
	internal static class ClientExceptionHelper
	{
		/// <summary>
		/// 	Throw an CrmArgumentOutOfRangeException if the specified integer is less than zero.
		/// </summary>
		/// <param name = "value">Integer value.</param>
		/// <param name = "parameterName">Name of the parameter.</param>
		internal static void ThrowIfNegative(int value, string parameterName)
		{
			if (value < 0)
			{
				throw new ArgumentOutOfRangeException(
					parameterName,
					"Value for this parameter must be equal or greater than zero.");
			}
		}

		/// <summary>
		/// 	Throw an XrmArgumentNullException if the specified parameter is null.
		/// </summary>
		/// <param name = "parameter">Parameter value.</param>
		/// <param name = "name">Parameter name.</param>
		internal static void ThrowIfNull(object parameter, string name)
		{
			if (parameter == null)
			{
				throw new ArgumentNullException(name);
			}
		}

		/// <summary>
		/// 	Throw an XrmArgumentNullException if the specified parameter is null.
		/// </summary>
		/// <param name = "parameter">Parameter value.</param>
		/// <param name = "name">Parameter name.</param>
		internal static void ThrowIfNullOrEmpty(string parameter, string name)
		{
			if (String.IsNullOrEmpty(parameter))
			{
				throw new ArgumentNullException(name);
			}
		}

		/// <summary>
		/// 	Throw an XrmArgumentException if the specified parameter is Guid.Empty.
		/// </summary>
		/// <param name = "parameter">Parameter value.</param>
		/// <param name = "name">Parameter name.</param>
		internal static void ThrowIfGuidEmpty(Guid parameter, string name)
		{
			if (parameter == Guid.Empty)
			{
				throw new ArgumentException("Expected non-empty Guid.", name);
			}
		}


		internal static string FormatMessage(int errorCode, params object[] arguments)
		{
			const string argument = "\nData[{0}] = \"{1}\"";
			string result;
			string message = errorCode.ToString(CultureInfo.InvariantCulture);
			try
			{
				result = string.Format(CultureInfo.InvariantCulture, message, arguments);
			}
			catch (FormatException)
			{
				StringBuilder sb = new StringBuilder(message);
				for (int i = 0; i < arguments.Length; i++)
				{
					sb.Append(string.Format(CultureInfo.InvariantCulture, argument, i, arguments[i]));
				}
				result = sb.ToString();
			}
			return result;
		}

		internal static void Assert(bool condition, string message, params object[] args)
		{
			Assert(condition, string.Format(CultureInfo.InvariantCulture, message, args));
		}

		internal static void Assert(bool condition, string message)
		{
			InvalidOperationException e = null;

			if (!condition)
			{
				e = new InvalidOperationException(message);
			}
#if DEBUG
			Debug.Assert(condition, message);
#endif
			if (null != e)
			{
				throw e;
			}
		}
	}
}
